﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TDTool.Utils;

namespace TDTool
{
    /// <summary>
    /// RocketWindow.xaml 的交互逻辑
    /// </summary>
    public partial class RocketWindow : Window
    {
        const int widgetWidth = 56;
        // 隐藏时，window的left值
        double Hidden_Location_For_Left = SystemParameters.PrimaryScreenWidth - widgetWidth/2;
        double Edge_Location_For_Left = SystemParameters.PrimaryScreenWidth - widgetWidth;
        const double ANIM_TIME = 200.0;
        bool IsMouseEnter = false;
        public RocketWindow()
        {
            InitializeComponent();
        }
        #region event
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitNoAltTabShow();
            FloatWindow_Location();
        }
        private void Window_MouseEnter(object sender, MouseEventArgs e)
        {
            IsMouseEnter = true;
            AnimToRightEdge();
        }

        private void Window_MouseLeave(object sender, MouseEventArgs e)
        {
            IsMouseEnter = false;
            AnimToRightEdgeHidden();
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            //AnimToRightEdge();
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            ReleaseAnim();
        }

        private void CloseMenuItem_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        #endregion




        // Alt+Tab时 不显示该Window
        private void InitNoAltTabShow()
        {
            WindowInteropHelper wndHelper = new WindowInteropHelper(this);
            WindowHelper.Window_Loaded(wndHelper);
        }
        // 窗体在加载后，控制它显示的位置。
        void FloatWindow_Location()
        {
            this.Left = SystemParameters.PrimaryScreenWidth - 56;
            this.Top = SystemParameters.PrimaryScreenHeight - 300;
            AnimToRightEdgeHidden();
        }

        #region Anim
        private void AnimToRightEdge()
        {
            Console.WriteLine("AnimToRightEdge " + RootWindow.Left);
            if (!IsMouseEnter || RootWindow.Left == Edge_Location_For_Left)
            {
                return;
            }
            DoubleAnimation locationAnim = new DoubleAnimation();
            locationAnim.To = Edge_Location_For_Left;
            Console.WriteLine("AnimToRightEdge To:" + locationAnim.To);
            //locationAnim.Completed += (s, e) => ReleaseAnim();
            locationAnim.Duration = TimeSpan.FromMilliseconds(ANIM_TIME);
            RootWindow.BeginAnimation(Window.LeftProperty, locationAnim);
        }
        private void AnimToRightEdgeHidden()
        {
            Console.WriteLine("AnimToRightEdgeHidden " + RootWindow.Left);
            if (IsMouseEnter || RootWindow.Left == Hidden_Location_For_Left)
            {
                return;
            }
            DoubleAnimation locationAnim = new DoubleAnimation();
            locationAnim.To = Hidden_Location_For_Left;
            Console.WriteLine("AnimToRightEdgeHidden To:" + locationAnim.To);
            //locationAnim.Completed += (s, e) =>
            //{
            //    ReleaseAnim();
            //};
            locationAnim.Duration = TimeSpan.FromMilliseconds(ANIM_TIME);
            RootWindow.BeginAnimation(Window.LeftProperty, locationAnim);
        }
        /// <summary>
        /// 动画的优先级高，如果不释放，则RootWindow.Left的值始终是动画之后的值
        /// 故在动画结束后取消关联
        ///   ==> 有bug。先不用了
        /// </summary>
        private void ReleaseAnim()
        {
            RootWindow.BeginAnimation(Window.LeftProperty, null);
        }
        #endregion
    }
}
